package com.scalar.chiptrack.adaptor.asecl;

import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * User: ChandraSekhar
 * Date: May 22, 2008
 * Time: 2:46:44 PM
 */
public class ASECLDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap m_sortDataMap           = new HashMap();
    private HashMap m_dieBankDataMap        = new HashMap();
    private HashMap m_assemblyDataMap       = new HashMap();
    private HashMap m_ftDataMap           = new HashMap();
    private HashMap m_waferBankDataMap     = new HashMap();
    private HashMap m_ftShipDataMap       = new HashMap();

    private Logger m_parserLogger   = null;
    private String m_dataOutputDir  = null;

    public ASECLDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir(VENDOR_NAME_ASECL, WIP_TYPE_ASSEMBLY);
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir, VENDOR_NAME_ASECL );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );
            TestAdaptorDAO.setLogger( m_parserLogger );
            InventoryLotDAO.setLogger(m_parserLogger);
            FabSortAdaptorDAO.setLogger(m_parserLogger);
            ShippedLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading ASECL WIP output directory details from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new ASECLDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_ASECL, CONSTANT_PARSER);

        File doneDestDir = new File(  m_dataOutputDir + "/done" );

        File sourceDataoutDir = new File( m_dataOutputDir );

        File[] fileList = sourceDataoutDir.listFiles();
        if ( ( fileList == null ) || ( fileList.length <= 3 ) )
        {
            log( "No file found in directory: " + sourceDataoutDir );
            throw new ScalarException( "\n+-----------No file found in directory " + sourceDataoutDir );
        }

        ArrayList successFileList = new ArrayList();

        boolean success = false;

        for ( int i = 0; i < fileList.length; i++ )
        {
            if ( fileList[i].isDirectory() == true )
            {
                continue;
            }

            try
            {
                // Step 1
                // Parse Files to get Object List
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASECLAdaptor.FILE_NAME_TURNKEY_STATUS ) )
                {
                    // To parse the the ASECL_WIP_STATUS sheet in to table
                    ArrayList aseclWipList = parseTurnKeyWipReport( fileList[i].getAbsolutePath() );
                    
                    if ( aseclWipList != null && aseclWipList.size() > 0 )
                    {
                        //to get the ASECL Wip Details details
                        getSheetDetails( aseclWipList );
                    }

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASECLAdaptor.FILE_NAME_SHIP_STATUS ) )
                {
                    // To parse the the ASECL_WIP_STATUS sheet in to table
                    ArrayList aseclShipList = parseShipmentReport( fileList[i].getAbsolutePath() );

                    if ( aseclShipList != null && aseclShipList.size() > 0 )
                    {
                        //to get the ASECL Ship Details details
                        getShippingDetails( aseclShipList );
                    }

                    successFileList.add( fileList[i] );
                }
                success = true;
            }
            catch ( Exception e )
            {
                success = false;
                log( "Exception while parsing and storing file: " + fileList[i].getAbsolutePath() + "\nException: " + e );
                e.printStackTrace();
                throw e;
            }
        }
        /*Updating the Wafer Store Lot Information*/
        try
        {
            // Save Wafer Store Information
            if (m_waferBankDataMap.isEmpty() == false)
            {
                log(" Updating Wafer Store Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_waferBankDataMap.values().iterator(), LOCATION_TYPE_WAFER_STORE, VENDOR_NAME_ASECL);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_WAFER_STORE, VENDOR_NAME_ASECL);
            }
            success = true;
            m_waferBankDataMap.clear();
            m_waferBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
            throw se;
        }

        //to saving sort Lot Info
        try
        {
            // Save Sort Information
            if ( m_sortDataMap.isEmpty() == false )
            {
                log( " Updating Sort Lot Information" );
                FabSortAdaptorDAO.saveSortLot( null, m_sortDataMap.values().iterator(), WIP_TYPE_SORT, VENDOR_NAME_ASECL );
            }
            else
            {
                FabSortAdaptorDAO.closeFabSortLot( null, WIP_TYPE_SORT, VENDOR_NAME_ASECL );
            }
            success = true;
            m_sortDataMap.clear();
            m_sortDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Sort Lot information into database: " + se );
            throw se;
        }

        /*Updating the Die Bank Lot Information*/
        try
        {
            // Save Die Bank Information
            if (m_dieBankDataMap.isEmpty() == false)
            {
                log(" Updating Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_dieBankDataMap.values().iterator(), LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASECL);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASECL);
            }
            success = true;
            m_dieBankDataMap.clear();
            m_dieBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
            throw se;
        }
        /*Assembly Lot Saving*/
        try
        {
            // Save Assembly Information
            if ( m_assemblyDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Lot Information" );
                AssemblyAdaptorDAO.saveAssemblyLot( null, m_assemblyDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASECL );
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot( null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASECL);
            }
            success = true;
            m_assemblyDataMap.clear();
            m_assemblyDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se );
            throw se;
        }
        // Saving the Test Lot Information
        try
        {
            // Save Final Test Information
            if ( m_ftDataMap.isEmpty() == false )
            {
                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_ASECL );
            }
            else
            {
                TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_ASECL );
            }

            success = true;
            m_ftDataMap.clear();
            m_ftDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se );
            throw se;
        }

        try
        {
            // Save Final Test Information
            if ( m_ftShipDataMap.isEmpty() == false )
            {
                log( " Updating Test Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_ftShipDataMap.values().iterator() );
            }
            success = true;
            m_ftShipDataMap.clear();
            m_ftShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Shipping Lot information into database: " + se );
            throw se;
        }
         // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_ASECL, true, true, true);

        // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots(VENDOR_NAME_ASECL);


       int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( ASECLAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASECLAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASECLAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }


    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseTurnKeyWipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList wipList              = new ArrayList();
        String line                     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Turn Key Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Turn Key Wip Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( detailWipFile ) );
            reader.readLine();  // Read out <Header Info > Line

            while ( ( line = reader.readLine() ) != null  && line.length() > 0 )
            {
                ASECLWipStatus wipStatus = new ASECLWipStatus();

                line = line.replaceAll( "\"", "" );
                String[] token = line.split( "," );

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                    wipStatus.setDeviceName( token[x].trim() );
                                    break;
                            case 1:
									wipStatus.setWaferLotNo( token[x].trim() );
                                    break;
                            case 2:
                                    wipStatus.setFabLotNumber( token[x].trim() );
                                    break;
                            case 3:
                                    wipStatus.setPoNumber( token[x].trim() );
                                    break;
                            case 4:
                                    wipStatus.setVendorLotNo( token[x].trim());
                                    break;
                            case 5:
                                    wipStatus.setPkgType( token[x].trim());
                                    break;
                            case 6:
                                    wipStatus.setLeadCount( token[x].trim());
                                    break;
                            //7 and 8 , 9 not required

                            case 10:
                                    wipStatus.setCpIntra( token[x].trim());
                                    break;
                            case 11:
                                    wipStatus.setCpRecvDate( token[x].trim());
                                    break;
                            case 12:
                                    wipStatus.setCpIssueDate ( token[x].trim());
                                    break;
                            case 13:
                                    wipStatus.setCpUnIssueQty( token[x].trim() );
                                    break;
                            case 14:
                                    wipStatus.setCp1 ( token[x].trim());
                                    break;
                            //15, 16, 17, 18
                            case 19:
                                    wipStatus.setCpQA( token[x].trim() );
                                    break;
                            case 20:
                                    wipStatus.setCpWP( token[x].trim());
                                    break;
                            case 21:
                                    wipStatus.setCpElseA( token[x].trim());
                                    break;
                            case 22:
                                    wipStatus.setCpHold( token[x].trim());
                                    break;
                            case 23:
                                    wipStatus.setCpSOD( token[x].trim());
                                    break;
                            case 24:
                                    wipStatus.setCpFgBank( token[x].trim());
                                    break;
                            case 25:
                                    wipStatus.setAssyIntra( token[x].trim());
                                    break;
                            case 26:
                                    wipStatus.setUnissueWBank( token[x].trim());
                                    break;
                            case 27:
                                    wipStatus.setUnissueDBank(token[x].trim());
                                    break;
                            case 28:
                                    wipStatus.setAssyRecvDate(token[x].trim());
                                    break;
                            case 29:
                                    wipStatus.setAssyIssueDate(token[x].trim());
                                    break;
                            case 30:
                                    wipStatus.setGrind(token[x].trim());
                                    break;
                            case 31:
                                    wipStatus.setDieBond(token[x].trim());
                                    break;
                            case 32:
                                    wipStatus.setWireBond(token[x].trim());
                                    break;
                            case 33:
                                    wipStatus.setMolding(token[x].trim());
                                    break;
                            case 34:
                                    wipStatus.setMarking(token[x].trim());
                                    break;
                            case 35:
                                    wipStatus.setPlating( token[x].trim());
                                    break;
                            case 36:
                                    wipStatus.setFoaming(token[x].trim());
                                    break;
                            case 37:
                                    wipStatus.setFvi(token[x].trim());
                                    break;
                            case 38:
                                    wipStatus.setAssyHold( token[x].trim());
                                    break;
                            case 39:
                                    wipStatus.setAssySOD(token[x].trim());
                                    break;
                            case 40:
                                    wipStatus.setAssyRevisedSOD(token[x].trim());
                                    break;
                            case 41:
                                    wipStatus.setAssyHoldCode(token[x].trim());
                                    break;
                            case 42:
                                    wipStatus.setAssyHoldReason(token[x].trim());
                                    break;
                            //43 not Requiered
                            case 44:
                                    wipStatus.setFtIntra(token[x].trim());
                                    break;
                            case 45:
                                    wipStatus.setFtRecvDate(token[x].trim());
                                    break;
                            case 46:
                                    wipStatus.setFtIssueDate(token[x].trim());
                                    break;
                            case 47:
                                    wipStatus.setFtUnIssue(token[x].trim());
                                    break;
                            case 48:
                                    wipStatus.setFt1(token[x].trim());
                                    break;

                            //49, 50, 51,52,53 not required
                            case 54:
                                    wipStatus.setFtHold( token[x].trim() );
                                    break;
                            //55,56 not required
                            case 57:
                                    wipStatus.setVm(token[x].trim());
                                    break;
                            case 58:
                                    wipStatus.setBaking(token[x].trim());
                                    break;
                            case 59:
                                    wipStatus.setTapeReel(token[x].trim());
                                    break;
                            case 60:
                                    wipStatus.setPaking( token[x].trim() );
                                    break;
                            case 61:
                                    wipStatus.setFtElse(token[x].trim());
                                    break;
                            case 62:
                                    wipStatus.setBeHold(token[x].trim());
                                    break;
                            case 63:
                                    wipStatus.setFtSOD(token[x].trim());
                                    break;
                            case 64:
                                    wipStatus.setFtRevisedSOD( token[x].trim() );
                                    break;
                            case 65:
                                    wipStatus.setFtHoldCode(token[x].trim());
                                    break;
                            case 66:
                                    wipStatus.setFtHoldReason(token[x].trim());
                                    break;
                            case 67:
                                    wipStatus.setFtFgBank( token[x].trim() );
                                    break;
                           //68,69,70 not required
                         }

                    }

                }
                wipList.add( wipStatus );

            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch ( Exception ex )
        {   System.out.println("ERROR!!!ERROR!!! While Parsing the ASECL Turnkey Wip Report");
            ex.printStackTrace();
            throw ex;
        }

        return wipList;

    } // end of prase Turnkey Wip Report method

    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseShipmentReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList shipList              = new ArrayList();
        String line                     = null;

        try
        {
            File detailShipFile = new File( fileName );
            if ( detailShipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Shipment Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Shipment Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( detailShipFile ) );
            reader.readLine();  // Read out <Header Info > Line

            while ( ( line = reader.readLine() ) != null  && line.length() > 0 )
            {
                ASECLShipStatus shipStatus = new ASECLShipStatus();

                line = line.replaceAll( "\"", "" ).replaceAll("\\.,", ".");
                String[] token = line.split( "," );

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0)
                    {
                        switch (x)
                        {
                            case 0:
                                    shipStatus.setInvoiceNo( token[x].trim() );
                                    break;
                            case 1:
									shipStatus.setReleaseNo( token[x].trim() );
                                    break;
                            case 2:
                                    shipStatus.setFabLotNo( token[x].trim() );
                                    break;
                            case 3:
                                    shipStatus.setVendorLotNo( token[x].trim() );
                                    break;
                            case 4:
                                    shipStatus.setCustLotNo(token[x].trim());
                                    break;
                            case 5:
                                    shipStatus.setDeviceName( token[x].trim());
                                    break;
                            case 6:
                                    shipStatus.setPkgType( token[x].trim());
                                    break;
                            case 7:
                                    shipStatus.setLeadCount( token[x].trim());
                                    break;
                            case 8:
                                    shipStatus.setInvoiceDate( token[x].trim());
                                    break;
                            case 9:
                                    shipStatus.setShipDate( token[x].trim());
                                    break;
                            case 10:
                                    shipStatus.setShipOutQty( token[x].trim());
                                    break;
                            case 11:
                                    shipStatus.setMawbNo ( token[x].trim());
                                    break;
                            case 12:
                                    shipStatus.setHawbNo( token[x].trim() );
                                    break;
                            case 13:
                                    shipStatus.setFlightNo ( token[x].trim());
                                    break;
                            case 14:
                                    shipStatus.setNextLocation( token[x].trim() );
                                    break;
                            case 15:
                                    shipStatus.setEtd( token[x].trim());
                                    break;
                            case 16:
                                    shipStatus.setEta( token[x].trim().replaceAll("[a-zA-Z]", ""));
                                    break;
                            case 17:
                                //System.out.println("CASE 17 = " + token[x].trim() );
                                    break;
                            case 18:
                                //System.out.println("CASE 18 = " + token[x].trim());
                                    break;
                            case 19:
                                //System.out.println("CASE 19 = " + token[x].trim());
                                    shipStatus.setLotType( token[x].trim() );
                                    break;
                         }

                    }

                }
                shipList.add( shipStatus );

            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {

            }

        }
        catch ( Exception ex )
        {   System.out.println("ERROR!!!ERROR!!! While Parsing the ASECL Shipment Report");
            ex.printStackTrace();
            throw ex;
        }

        return shipList;

    } // end of prase Shipment Report method
    /**
     *
     * @param lotstatusList
     * @throws Exception
     */
    private void getSheetDetails( ArrayList lotstatusList ) throws Exception
    {
        ASECLWipStatus wipStatus = null;
        FabSortLot fabSortLot  = null;
        AssemblyLot assemblyLot = null;
        TestLot testLot         = null;
        InventoryLot dbInventoryLot = null;
        InventoryLot wsInventoryLot = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fablotNumber     = null;

        if (lotstatusList == null)
        {
            return;
        }

        Iterator iterator = lotstatusList.iterator();
        try
        {
            while (iterator.hasNext())
            {
                wipStatus = (ASECLWipStatus) iterator.next();

                fablotNumber    = wipStatus.getFabLotNumber();
                partNumber      = wipStatus.getDeviceName();
                lotNumber       = wipStatus.getVendorLotNo();

                if( lotNumber == null )
                {
                    lotNumber = fablotNumber;
                }

                if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                {
                    /* Wafer Store Lots*/
                    if (wipStatus.getCpIntra() != null && !"0".equalsIgnoreCase( wipStatus.getCpIntra() )  )
                    {
                        wsInventoryLot = (InventoryLot) m_waferBankDataMap.get(partNumber + fablotNumber);
                        if (wsInventoryLot == null)
                        {
                            wsInventoryLot = new InventoryLot(VENDOR_NAME_ASECL, LOCATION_TYPE_WAFER_STORE );
                            m_waferBankDataMap.put(partNumber + fablotNumber, wsInventoryLot);
                        }

                        int quantity = 0;
                        if (wsInventoryLot.getWaferQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt( wsInventoryLot.getWaferQtyIn() );
                            }
                            catch (NumberFormatException npe) {
                            }
                            try
                            {
                                quantity += Integer.parseInt( wipStatus.getCpIntra() );
                            }
                            catch (NumberFormatException npe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getCpIntra());
                            }
                            catch (NumberFormatException npe) {
                            }
                        }

                        if (quantity > 0)
                        {
                            wsInventoryLot.setLotNumber(fablotNumber);
                            wsInventoryLot.setPartNumber(partNumber);
                            wsInventoryLot.setDateIn(wipStatus.getCpRecvDate() );
                            wsInventoryLot.setWaferQtyIn(quantity + "");
                            wsInventoryLot.setStatus(LOT_STATUS_RUNNING);
                            wsInventoryLot.setFabLotNumber(fablotNumber);
                            wsInventoryLot.setCategory(LOT_CATEGORY_TYPE_PROD);
                        }
                    }

                    //SORT
                    if ( wipStatus.getCpUnIssueQty() != null && !"0".equalsIgnoreCase(wipStatus.getCpUnIssueQty())
                           || wipStatus.getCp1() != null && !"0".equalsIgnoreCase(wipStatus.getCp1())
                            || wipStatus.getCpQA() != null && !"0".equalsIgnoreCase(wipStatus.getCpQA())
                            || wipStatus.getCpWP() != null && !"0".equalsIgnoreCase(wipStatus.getCpWP())
                            || wipStatus.getCpElseA() != null && !"0".equalsIgnoreCase(wipStatus.getCpElseA())
                            || wipStatus.getCpHold() != null && !"0".equalsIgnoreCase(wipStatus.getCpHold())
                            || wipStatus.getCpFgBank() != null && !"0".equalsIgnoreCase(wipStatus.getCpFgBank()) )
                    {
                        log( lotNumber + "in Sort ");

                        fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                        if (fabSortLot == null)
                        {
                            fabSortLot = new FabSortLot(VENDOR_NAME_ASECL, WIP_TYPE_SORT );
                            m_sortDataMap.put( partNumber + fablotNumber, fabSortLot);
                        }

                        if (wipStatus.getCpUnIssueQty() != null && !"0".equalsIgnoreCase(wipStatus.getCpUnIssueQty() ) )
                        {
                            fabSortLot = (FabSortLot) m_sortDataMap.get(partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpUnIssueQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpUnIssueQty() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpUnIssueQty() );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", UnIssue") ;
                            }
                            else
                            {
                                fabSortLot.setStage("UnIssue");
                            }

                        }

                        if ( wipStatus.getCp1() != null && !"0".equalsIgnoreCase(wipStatus.getCp1() ) )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCp1() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCp1() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCp1() );
                            }

                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP1") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP1");
                            }
                        }

                        if ( wipStatus.getCpQA() != null && !"0".equalsIgnoreCase(wipStatus.getCpQA() )  )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpQA() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpQA() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpQA() );
                            }

                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", QA") ;
                            }
                            else
                            {
                                fabSortLot.setStage("QA");
                            }
                        }

                        if (wipStatus.getCpWP() != null && !"0".equalsIgnoreCase(wipStatus.getCpWP() ) )
                        {
                            fabSortLot = (FabSortLot) m_sortDataMap.get(partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpWP() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpWP() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpWP() );
                            }

                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", WP") ;
                            }
                            else
                            {
                                fabSortLot.setStage("WP");
                            }
                        }

                        if ( wipStatus.getCpElseA() != null && !"0".equalsIgnoreCase(wipStatus.getCpElseA() ) )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpElseA() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpElseA() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpElseA() );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", ELSEA") ;
                            }
                            else
                            {
                                fabSortLot.setStage("ELSEA");
                            }
                        }

                        if ( wipStatus.getCpHold() != null && !"0".equalsIgnoreCase(wipStatus.getCpHold() )  )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpHold() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpHold() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpHold() );
                            }

                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", Hold") ;
                            }
                            else
                            {
                                fabSortLot.setStage("Hold");
                            }
                        }
                        if ( wipStatus.getCpFgBank() != null && !"0".equalsIgnoreCase(wipStatus.getCpFgBank() ) && Integer.parseInt(wipStatus.getCpFgBank()) <= 25 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fablotNumber );

                            if( fabSortLot.getQtyIn() != null )
                            {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + Integer.parseInt( wipStatus.getCpFgBank() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( wipStatus.getCpFgBank() );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( wipStatus.getCpFgBank() );
                            }

                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", FgBank") ;
                            }
                            else
                            {
                                fabSortLot.setStage("FgBank");
                            }
                        }

                        fabSortLot.setCustProduct( partNumber );
                        fabSortLot.setLotId( fablotNumber );
                        fabSortLot.setForcastedDateOut( wipStatus.getCpSOD() );
                        fabSortLot.setPkgType( wipStatus.getPkgType() );
                        fabSortLot.setLeadCount( wipStatus.getLeadCount() );
                        fabSortLot.setDateIn( wipStatus.getCpIssueDate() );
                        fabSortLot.setFabLotNumber( fablotNumber );
                        fabSortLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                        if ( wipStatus.getCpHold() != null && !"0".equalsIgnoreCase(wipStatus.getCpHold() )  )
                        {
                            fabSortLot.setLotStatus( LOT_STATUS_HOLD );
                        }
                        else
                        {
                            fabSortLot.setLotStatus( LOT_STATUS_RUNNING );
                        }

                    }

                    //DIE BANK
                    if ( wipStatus.getUnissueDBank() != null && !"0".equalsIgnoreCase( wipStatus.getUnissueDBank() ) )
                    {
                        //if ( wipStatus.getUnissueWBank() != null && !"0".equalsIgnoreCase(wipStatus.getAsBkWafer())

                        log( lotNumber +" In Die Bank");

                        dbInventoryLot = (InventoryLot)m_dieBankDataMap.get( partNumber + lotNumber + fablotNumber );

                        if ( dbInventoryLot == null )
                        {
                            dbInventoryLot = new InventoryLot( VENDOR_NAME_ASECL, LOCATION_TYPE_DIE_BANK );
                            m_dieBankDataMap.put( partNumber + lotNumber + fablotNumber, dbInventoryLot );
                        }

                        int quantity = 0;
                        if (dbInventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(dbInventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getUnissueDBank());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getUnissueDBank() );
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }

                        dbInventoryLot.setPartNumber( partNumber );
                        dbInventoryLot.setLotNumber( lotNumber );
                        dbInventoryLot.setQtyIn(quantity + "");
                        dbInventoryLot.setDateIn( wipStatus.getAssyRecvDate() );
                        dbInventoryLot.setFabLotNumber( fablotNumber );
                        dbInventoryLot.setPkgType( wipStatus.getPkgType() );
                        dbInventoryLot.setLeadCount( wipStatus.getLeadCount() );
                        dbInventoryLot.setStatus( LOT_STATUS_RUNNING );
                        dbInventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                    }

                     //ASSEMBLY
                    if (  wipStatus.getAssyIntra() != null && wipStatus.getAssyIntra().equalsIgnoreCase("0") == false
                            || wipStatus.getGrind() != null && wipStatus.getGrind().equalsIgnoreCase("0") == false
                            || wipStatus.getWireBond() != null && wipStatus.getWireBond().equalsIgnoreCase("0") == false
                            || wipStatus.getMolding() != null && wipStatus.getMolding().equalsIgnoreCase("0") == false
                            || wipStatus.getPlating() != null && wipStatus.getPlating().equalsIgnoreCase("0") == false
                            || wipStatus.getDieBond() != null && wipStatus.getDieBond().equalsIgnoreCase("0") == false
                            || wipStatus.getMarking() != null && wipStatus.getMarking().equalsIgnoreCase("0") == false
                            || wipStatus.getFoaming() != null && wipStatus.getFoaming().equalsIgnoreCase("0") == false
                            || wipStatus.getFvi() != null && wipStatus.getFvi().equalsIgnoreCase("0") == false
                            || wipStatus.getAssyHold() != null && wipStatus.getAssyHold().equalsIgnoreCase("0") == false )
                   {
                        log( "In Assembly" + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assemblyDataMap.get( partNumber + lotNumber + fablotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot(VENDOR_NAME_ASECL );
                            m_assemblyDataMap.put( partNumber + lotNumber + fablotNumber, assemblyLot);
                        }

                        // FOR SETTING THE QTY IN
                        if( wipStatus.getAssyIntra() != null && wipStatus.getAssyIntra().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getAssyIntra() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getAssyIntra() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getAssyIntra() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Assy Intra") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Assy Intra");
                            }
                        }
                        if( wipStatus.getGrind() != null && wipStatus.getGrind().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getGrind() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getGrind() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getGrind() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Grinding") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Grinding");
                            }
                        }
                        if( wipStatus.getWireBond() != null && wipStatus.getWireBond().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getWireBond() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getWireBond() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getWireBond() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Wire Bond") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Wire Bond");
                            }
                        }
                        if( wipStatus.getMolding() != null && wipStatus.getMolding().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);

                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMolding() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMolding() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMolding() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Molding") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Molding");
                            }
                        }

                        if( wipStatus.getPlating() != null && wipStatus.getPlating().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getPlating() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getPlating() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getPlating() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Plating") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Plating");
                            }
                        }

                        if( wipStatus.getDieBond() != null && wipStatus.getDieBond().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDieBond() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDieBond() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDieBond() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", DieBond") ;
                            }
                            else
                            {
                                assemblyLot.setStage("DieBond");
                            }
                        }

                        if( wipStatus.getMarking() != null && wipStatus.getMarking().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMarking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMarking() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMarking() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Marking") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Marking");
                            }
                        }
                        if( wipStatus.getFoaming() != null && wipStatus.getFoaming().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFoaming() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFoaming() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFoaming() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Forming") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Forming");
                            }
                        }
                       if( wipStatus.getFvi() != null && wipStatus.getFvi().equalsIgnoreCase("0") == false )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFvi() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFvi() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFvi() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", FVI") ;
                            }
                            else
                            {
                                assemblyLot.setStage("FVI");
                            }
                        }
                        if( wipStatus.getAssyHold() != null && wipStatus.getAssyHold().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getAssyHold() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getAssyHold() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getAssyHold() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Hold") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Hold");
                            }
                        }

                        assemblyLot.setLotNumber( lotNumber );
                        assemblyLot.setDeviceName( partNumber );
                        if ( wipStatus.getAssyHold() != null && wipStatus.getAssyHold().equalsIgnoreCase("0") == false )
                        {
                           assemblyLot.setStatus(LOT_STATUS_HOLD);
                           assemblyLot.setHoldReason( wipStatus.getAssyHoldReason() );
                           assemblyLot.setHoldCode( wipStatus.getAssyHoldCode() );
                        }
                        else
                        {
                           assemblyLot.setStatus(LOT_STATUS_RUNNING);
                        }
                        assemblyLot.setFabLotNumber( fablotNumber );
                        assemblyLot.setPackageType( wipStatus.getPkgType() );
                        assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                        assemblyLot.setLeadCount( wipStatus.getLeadCount() );
                        if ( wipStatus.getAssyIssueDate() != null && wipStatus.getAssyIssueDate().trim().length() < 10 )
                        {
                            assemblyLot.setDateIn( StringUtils.formatDate(wipStatus.getAssyIssueDate(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        else
                        {
                            assemblyLot.setDateIn( StringUtils.formatDate(wipStatus.getAssyRecvDate(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        if (wipStatus.getAssyRevisedSOD() != null && wipStatus.getAssyRevisedSOD().trim().length() < 10  )
                        {
                            assemblyLot.setExpectedDateOut( StringUtils.formatDate(wipStatus.getAssyRevisedSOD(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        else
                        {
                             assemblyLot.setExpectedDateOut( StringUtils.formatDate(wipStatus.getAssySOD(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }

                        assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );
                    }

                    // FT Process
                    if ( wipStatus.getFtIntra() != null && !"0".equalsIgnoreCase( wipStatus.getFtIntra() )
                        || wipStatus.getFtUnIssue() != null && !"0".equalsIgnoreCase( wipStatus.getFtUnIssue() )
                        || wipStatus.getFt1() != null && !"0".equalsIgnoreCase( wipStatus.getFt1() )
                        || wipStatus.getFtHold() != null && !"0".equalsIgnoreCase( wipStatus.getFtHold() )
                        || wipStatus.getVm() != null && !"0".equalsIgnoreCase( wipStatus.getVm() )
                        || wipStatus.getBaking() != null && !"0".equalsIgnoreCase( wipStatus.getBaking() )
                        || wipStatus.getTapeReel() != null && !"0".equalsIgnoreCase( wipStatus.getTapeReel() )
                        || wipStatus.getPaking() != null && !"0".equalsIgnoreCase( wipStatus.getPaking() )
                        || wipStatus.getFtElse() != null && !"0".equalsIgnoreCase( wipStatus.getFtElse() )
                        || wipStatus.getBeHold() != null && !"0".equalsIgnoreCase( wipStatus.getBeHold() )
                        || wipStatus.getFtFgBank() != null && !"0".equalsIgnoreCase( wipStatus.getFtFgBank() )    )
                    {
                        log(lotNumber + "In Test FT");

                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                        if (testLot == null)
                        {
                            testLot = new TestLot(VENDOR_NAME_ASECL);
                            m_ftDataMap.put(partNumber + lotNumber + fablotNumber, testLot);
                        }

                       // FOR SETTING THE QTY IN
                       if( wipStatus.getFtIntra() != null && wipStatus.getFtIntra().equalsIgnoreCase("0") == false )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFtIntra() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFtIntra() );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( wipStatus.getFtIntra() );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FtIntra");
                            }
                            else
                            {
                                testLot.setStage("FtIntra");
                            }
                        }
                        if( wipStatus.getFtUnIssue() != null && wipStatus.getFtUnIssue().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFtUnIssue() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFtUnIssue() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getFtUnIssue() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", UnIssue");
                             }
                             else
                             {
                                testLot.setStage("UnIssue");
                             }
                        }
                       if( wipStatus.getFt1() != null && wipStatus.getFt1().equalsIgnoreCase("0") == false )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFt1() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFt1() );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( wipStatus.getFt1() );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FT1");
                            }
                            else
                            {
                                testLot.setStage("FT1");
                            }
                        }
                        if( wipStatus.getFtHold() != null && wipStatus.getFtHold().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFtHold() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFtHold() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getFtHold() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", FTHold");
                             }
                             else
                             {
                                testLot.setStage("FTHold");
                             }
                        }
                        if( wipStatus.getVm() != null && wipStatus.getVm().equalsIgnoreCase("0") == false )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getVm() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getVm() );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( wipStatus.getVm() );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", VM");
                            }
                            else
                            {
                                testLot.setStage("VM");
                            }
                        }
                        if( wipStatus.getBaking() != null && wipStatus.getBaking().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getBaking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getBaking() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getBaking() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", Baking");
                             }
                             else
                             {
                                testLot.setStage("Baking");
                             }
                        }
                       if( wipStatus.getTapeReel() != null && wipStatus.getTapeReel().equalsIgnoreCase("0") == false )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getTapeReel() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getTapeReel() );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( wipStatus.getTapeReel() );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", TR");
                            }
                            else
                            {
                                testLot.setStage("TR");
                            }
                        }
                        if( wipStatus.getPaking() != null && wipStatus.getPaking().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getPaking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getPaking() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getPaking() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", Paking");
                             }
                             else
                             {
                                testLot.setStage("Paking");
                             }
                        }
                        if( wipStatus.getFtElse() != null && wipStatus.getFtElse().equalsIgnoreCase("0") == false )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFtElse() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFtElse() );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( wipStatus.getFtElse() );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FtElse");
                            }
                            else
                            {
                                testLot.setStage("FtElse");
                            }
                        }
                        if( wipStatus.getBeHold() != null && wipStatus.getBeHold().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getBeHold() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getBeHold() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getBeHold() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", BE Hold");
                             }
                             else
                             {
                                testLot.setStage("BE Hold");
                             }
                        }
                        if( wipStatus.getFtFgBank() != null && wipStatus.getFtFgBank().equalsIgnoreCase("0") == false )
                        {
                            testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fablotNumber );
                            if( testLot.getQtyIn() != null )
                            {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFtFgBank() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( wipStatus.getFtFgBank() );
                                }
                             }
                             else
                             {
                                testLot.setQtyIn( wipStatus.getFtFgBank() );
                             }
                             if ( testLot.getStage() != null )
                             {
                                testLot.setStage( testLot.getStage() + ", Fg Bank");
                             }
                             else
                             {
                                testLot.setStage("Fg Bank");
                             }
                        }
                        testLot.setPartNumber( partNumber );
                        testLot.setLotNumber( lotNumber );
                        testLot.setFabLotNumber( fablotNumber );
                        if ( wipStatus.getFtIssueDate() != null && wipStatus.getFtIssueDate().length() < 10 )
                        {
                            testLot.setDateIn( StringUtils.formatDate(wipStatus.getFtIssueDate(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        else
                        {
                            testLot.setDateIn( StringUtils.formatDate(wipStatus.getFtRecvDate(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        if (wipStatus.getFtRevisedSOD() != null && wipStatus.getFtRevisedSOD().trim().length() < 10  )
                        {
                            testLot.setExpecetedDate( StringUtils.formatDate(wipStatus.getFtRevisedSOD(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        else
                        {
                            testLot.setExpecetedDate( StringUtils.formatDate(wipStatus.getFtSOD(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        }
                        testLot.setLeadCount( wipStatus.getLeadCount() );
                        testLot.setPackageType( wipStatus.getPkgType() );
                        if ( (wipStatus.getFtHold() != null && wipStatus.getFtHold().equalsIgnoreCase("0") == false )
                                || wipStatus.getBeHold() != null && wipStatus.getBeHold().equalsIgnoreCase("0") == false )
                        {
                           testLot.setStatus(LOT_STATUS_HOLD);
                           testLot.setHoldReason( wipStatus.getFtHoldReason() );
                           testLot.setHoldCode( wipStatus.getFtHoldCode() );
                        }
                        else
                        {
                           testLot.setStatus(LOT_STATUS_RUNNING);
                        }
                        testLot.setPsType( LOCATION_TYPE_FT );
                        testLot.setRevisedDateOut( StringUtils.formatDate(wipStatus.getFtRevisedSOD(), "yyyy/MM/dd", "MM/dd/yyyy" ) );
                        testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                    }
                   
                }
            }
        }
        catch( Exception e)
        {
            System.out.println(" !!ERROR !! ERROR Exception while get getting the ASECL Wip Details");
            e.printStackTrace();
            throw e;
        }
    }

    private void getShippingDetails(ArrayList shippingDataList) throws Exception
    {
       if ( shippingDataList == null || shippingDataList.size() == 0 )
       {
            return;
       }
       ASECLShipStatus shipStatus  = null;
       String partNumber                = null;
       String lotNumber                 = null;
       ShippedLot shippedLot          = null;
       String fabLotNumber              = null;
       String nextLocation              = null;
       try
       {
           Iterator iterator = shippingDataList.iterator();

           while ( iterator.hasNext() )
           {
               shipStatus = (ASECLShipStatus) iterator.next();

               lotNumber    = shipStatus.getVendorLotNo() ;
               partNumber   = shipStatus.getDeviceName();
               fabLotNumber = shipStatus.getFabLotNo();
               nextLocation = shipStatus.getNextLocation();
               String shipDate = null;

               if ( shipStatus.getShipDate() != null )
               {
                   shipDate = StringUtils.formatDate( shipStatus.getShipDate(), "yyyy/MM/dd", "MM/dd/yyyy");
               }

               if ( lotNumber != null && partNumber != null && fabLotNumber != null && shipStatus.getShipOutQty() != null && !"0".equalsIgnoreCase( shipStatus.getShipOutQty() ) )
               {
                   shippedLot = ( ShippedLot ) m_ftShipDataMap.get ( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate );

                   if ( shippedLot == null )
                   {
                        shippedLot = new ShippedLot(VENDOR_NAME_ASECL, LOCATION_TYPE_TEST);
                        m_ftShipDataMap.put( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate, shippedLot );
                   }

                   int quantity = 0;
                   if ( shippedLot.getShipQty() != null )
                   {
                       try
                       {
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                       try
                       {
                           quantity += Integer.parseInt( shipStatus.getShipOutQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                   }
                   else
                   {
                       try
                       {
                           quantity = Integer.parseInt( shipStatus.getShipOutQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                   }

                   if ( quantity > 0 )
                   {
                       shippedLot.setLotNumber( lotNumber );
                       shippedLot.setPartNumber( partNumber );
                       shippedLot.setShipDate( shipDate );
                       shippedLot.setShipQty( quantity + "" );
                       shippedLot.setPackageType( shipStatus.getPkgType() );
                       shippedLot.setLeadCount( shipStatus.getLeadCount() );
                       shippedLot.setNextLocation( nextLocation );
                       shippedLot.setInvoiceNumber( shipStatus.getInvoiceNo() );
                       shippedLot.setFabLotNumber( fabLotNumber );
                       shippedLot.setMawb( shipStatus.getMawbNo() );
                       shippedLot.setHawb( shipStatus.getHawbNo() );
                       shippedLot.setEtd( shipStatus.getEtd() );
                       shippedLot.setEta( shipStatus.getEta() );
                       shippedLot.setFlightNumber( shipStatus.getFlightNo() );
                       shippedLot.setProcessType( LOCATION_TYPE_FT );

                   }
               }
           }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            log("Exception while geting ASECL Shipping inforamtion" + e);
            throw  e;
        }
    }

    private void log( String message )
    {
        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            m_parserLogger.logDebug( message + LINE_SEPARATOR );
        }
        else
        {
            System.out.println( message );
        }
    } // end of log method
}